package com.example.demo;

import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.FormatKeys.KeyFrameIntervalKey;
import static org.monte.media.FormatKeys.MIME_AVI;
import static org.monte.media.FormatKeys.MediaTypeKey;
import static org.monte.media.FormatKeys.MimeTypeKey;
import static org.monte.media.VideoFormatKeys.*;


import java.awt.*;
import java.io.File;
import org.joda.time.DateTime;
import org.monte.media.Format;
import org.monte.media.FormatKeys;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;


/**
 * @author kirk.dai
 * @date 2019/8/8.
 */
public class ScreenRecordDemo {


    public static  ScreenRecorder getTSCCScreenRecoder(GraphicsConfiguration graphicsConfiguration,File destfileDir) throws Exception {
        ScreenRecorder screenRecorder = new ScreenRecorder(
                //  显卡配置	提供了有关显示画面，例如大小和分辨率信息。 【cfg】
                graphicsConfiguration,
                // captureArea
                null,
                // 视频压缩格式	电影与帧/秒的数字输出格式。 【fileFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI),
                //  编码方式 TSCC/JEPG 【screenFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, (int) 24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (int) (15 * 60)),
                //鼠标光标和刷新速率的颜色	指定的鼠标光标的颜色和刷新速率 【mouseFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, "black", FrameRateKey, Rational.valueOf(30)),
                //音频格式	如果'NULL'音频不会被记录。 【audioFormat】
                null,
                //视频文件存放目录  【movieFolder】
                destfileDir);
        return screenRecorder;
    }

    public static ScreenRecorder getJPEGScreenRecoder(GraphicsConfiguration graphicsConfiguration,File destfileDir) throws Exception {
        ScreenRecorder screenRecorder = new ScreenRecorder(
                //  显卡配置	提供了有关显示画面，例如大小和分辨率信息。 【cfg】
                graphicsConfiguration,
                // captureArea
                null,
                // 视频压缩格式	电影与帧/秒的数字输出格式。 【fileFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.FILE, MimeTypeKey, MIME_QUICKTIME),
                //  编码方式 TSCC/JEPG 【screenFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_QUICKTIME_JPEG, CompressorNameKey, ENCODING_QUICKTIME_JPEG, DepthKey, (int) 24, FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (int) (15 * 60)),
                //鼠标光标和刷新速率的颜色	指定的鼠标光标的颜色和刷新速率 【mouseFormat】
                new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, "red", FrameRateKey, Rational.valueOf(30)),
                //音频格式	如果'NULL'音频不会被记录。 【audioFormat】
                null,
                //视频文件存放目录  movieFolder
                destfileDir);

        return screenRecorder;
    }


    public static void main(String[] args) throws Exception {


        GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

        File destfileDir = new File("d:\\screenRecoder\\"+ DateTime.now().toString("HHmmssSSS"));
        if(!destfileDir.exists()) {
            destfileDir.mkdirs();
        }

        //TSCC或者JPEG编码
//        ScreenRecorder screenRecorder = getTSCCScreenRecoder(gconfig,destfileDir);
        ScreenRecorder screenRecorder = getJPEGScreenRecoder(gconfig,destfileDir);


        System.out.println("Press ENTER to start audio recording.");
        while(System.in.read() != 10) {
            ;
        }

        System.out.println("start recording ...");
        screenRecorder.start();

        System.out.println("Press ENTER to stop  recording.");
        while(System.in.read() != 10) {
            ;
        }
        System.out.println("stop recording ...");
        screenRecorder.stop();
        if (screenRecorder.getState()!=null && screenRecorder.getState().equals(ScreenRecorder.State.RECORDING)) {
            System.out.println("state "+ screenRecorder.getState() +"  ,retry stop");
            screenRecorder.stop();
        }
        System.out.println("stop success");
        screenRecorder.getCreatedMovieFiles().forEach( file -> {
            System.out.println("You can find the recorded file:"+file.getAbsolutePath());
        });


    }
}
